/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import org.primefaces.component.commandlink.CommandLink;
import org.primefaces.context.RequestContext;

/**
 *
 * @author jasuke
 */
@ManagedBean
@SessionScoped
public class MenBean implements Serializable, ActionListener  {
    private String selection;
    private String id;

    public MenBean() {
        initModel();
    }

    
   @Override
    public void processAction(ActionEvent ae) throws AbortProcessingException 
    {
                CommandLink itm = (CommandLink)ae.getSource();
                String menu_name = itm.getValue().toString();
                selection = itm.getTitle().toString();
                String message = "CLICK ON MENU ITEM: " + menu_name;
                Severity severity = FacesMessage.SEVERITY_INFO;

                FacesMessage msg = new FacesMessage(severity, "PROGRAM SELECT", message);

                FacesContext.getCurrentInstance().addMessage(null, msg);

                RequestContext requestContext = RequestContext.getCurrentInstance();

                requestContext.addPartialUpdateTarget("frmContent:growl");
    }

    public class MenuList implements Serializable{

        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

    public class MenuTab implements Serializable {

        private String id;
        private String title;
        private List<MenuList> list;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public List<MenuList> getList() {
            return list;
        }

        public void setList(List<MenuList> list) {
            this.list = list;
        }
    }

    private List<MenuTab> menus = new ArrayList<MenuTab>();

    public List<MenuTab> getMenus() {
        return menus;
    }

    @PostConstruct
    private void initModel() {

        List<MenuList> list;
        MenuTab tab;
        MenuList item;

        for(int i=1; i< 4; i++){
                list = new ArrayList<MenuList>();
                tab = new MenuTab();        
                tab.setId("tab" + i);
                tab.setTitle("Menu "+i);

                for(int j=1; j<5; j++){
                      item = new MenuList();
                      item.setId(id);
                      item.setName("Submenu "+i+" - "+j);
        
                      list.add(item);
               }

               tab.setList(list);
               menus.add(tab);

       }

    }


}
